package view.user;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import entity.User;
import service.user.UserService;
import service.user.UserServiceImpl;
import utils.ComonUtil;
import view.MainView;
import view.CommonView.CommonJPanel;

import view.model.UserTableModel;
import view.model.UserTypeComboBoxModel;

/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 用户信息编辑模块
 *
 */
public class EditUserView extends CommonJPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// service层
	//    BookService service = new BookService();

	// 创建输入框
	private JTextField userIdField, userNameField, editUserIdField, editUserNameField, editUserPassField;
	//	UserTypeComboBoxModel bcm = new UserTypeComboBoxModel();
	private UserTypeComboBoxModel bcm1 = new UserTypeComboBoxModel();
	private JComboBox userTypeComboBox, roleComboBox, userTypeComboBoxEidt;

	// 创建表格
	private JTable table;
	private UserTableModel userTableModel;// 表格模板

	// 创建按钮
	private JButton editBtn, searchButton, deleteBtn;

	private String[] roleArr = { "读者", "管理员" };

	private static UserService userServiceImpl = new UserServiceImpl();

	public EditUserView() {
		super(TYPE_TRANSPARENT);
		setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
		setBorder(new EmptyBorder(20, 100, 10, 100));

		// 标题面板
		JPanel titlePanel = new CommonJPanel(TYPE_TRANSPARENT);
		add(titlePanel);
		// 内容标题
		JLabel labelAddBook = new JLabel("修改用户");
		labelAddBook.setFont(new Font("微软雅黑", Font.BOLD, 20));
		titlePanel.setPreferredSize(new Dimension(MainView.instance.getWidth(), 30));
		titlePanel.add(labelAddBook);

		// 标签、输入框面板
		JPanel searchPanel = new CommonJPanel(TYPE_TRANSPARENT);
		searchPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 20));
		searchPanel.setPreferredSize(new Dimension(MainView.instance.getWidth(), 70));
		add(searchPanel);

		// 创建用户ID标签（搜索）
		JLabel labelId = new JLabel("用户 ID：");
		searchPanel.add(labelId);
		// 创建用户ID输入框（搜索）
		userIdField = new JTextField();
		userIdField.setPreferredSize(new Dimension(120, 30));
		searchPanel.add(userIdField);

		// 创建用户昵称标签（搜索）
		JLabel labelName = new JLabel("用户名称：");
		searchPanel.add(labelName);
		// 创建用户昵称输入框（搜索）
		userNameField = new JTextField();
		userNameField.setPreferredSize(new Dimension(120, 30));
		searchPanel.add(userNameField);

		// 创建用户身份（输入框）
		//		JLabel labelBookType = new JLabel("用户身份：");
		//		searchPanel.add(labelBookType);
				// 创建用户身份选择框
		//		userTypeComboBox = new JComboBox<>(bcm);
		//		userTypeComboBox.setPreferredSize(new Dimension(120, 30));
		//		userTypeComboBox.setBackground(Color.white);
		//		searchPanel.add(userTypeComboBox);

		// 创建按钮
		searchButton = new JButton("查询");
		searchButton.setPreferredSize(new Dimension(100, 40));
		searchPanel.add(searchButton);

		// 创建表格面板
		JPanel tablePanel = new CommonJPanel(TYPE_TRANSPARENT);
		add(tablePanel);

		// 创建表格
		userTableModel = new UserTableModel();
		table = new JTable(userTableModel);
		JScrollPane sp = new JScrollPane(table);
		sp.setPreferredSize(new Dimension(750, 180));
		sp.setBackground(new Color(246, 246, 246));
		add(sp);

		// 数据显示区域面板
		JPanel editPanel = new CommonJPanel(TYPE_TRANSPARENT);
		editPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 20));
		editPanel.setPreferredSize(new Dimension(MainView.instance.getWidth(), 200));
		add(editPanel);

		// id
		JLabel labelUserID = new JLabel("用户  ID：");
		labelUserID.setPreferredSize(new Dimension(80, 30));
		editPanel.add(labelUserID);
		// 创建用户Id输入框
		editUserIdField = new JTextField();
		editUserIdField.setPreferredSize(new Dimension(220, 30));
		editUserIdField.setEditable(false);
		editPanel.add(editUserIdField);

		// 用户昵称
		JLabel labelUserName = new JLabel("用户昵称：");
		labelUserName.setPreferredSize(new Dimension(80, 30));
		editPanel.add(labelUserName);
		// 创建输入框
		editUserNameField = new JTextField();
		editUserNameField.setPreferredSize(new Dimension(220, 30));
		editPanel.add(editUserNameField);

		// 创建用户密码
		JLabel labelUserPass = new JLabel("用户密码：");
		labelUserPass.setPreferredSize(new Dimension(80, 30));
		editPanel.add(labelUserPass);
		// 创建用户密码
		editUserPassField = new JTextField();
		editUserPassField.setPreferredSize(new Dimension(220, 30));
		editPanel.add(editUserPassField);

		// 创建用户角色标签
		JLabel labelUserType = new JLabel("用户角色：");
		labelUserType.setPreferredSize(new Dimension(80, 30));
		editPanel.add(labelUserType);
		// 创建用户角色下拉框
		roleComboBox = new JComboBox<String>(roleArr);
		roleComboBox.setPreferredSize(new Dimension(220, 30));
		roleComboBox.setBackground(Color.white);
		editPanel.add(roleComboBox);

		// 身份
		JLabel labelUserTypeEd = new JLabel("用户身份：");
		labelUserTypeEd.setPreferredSize(new Dimension(80, 30));
		editPanel.add(labelUserTypeEd);
		userTypeComboBoxEidt = new JComboBox<>(bcm1);
		userTypeComboBoxEidt.setPreferredSize(new Dimension(MainView.instance.getWidth() - 280, 30));
		userTypeComboBoxEidt.setBackground(Color.white);
		editPanel.add(userTypeComboBoxEidt);

		// 创建按钮面板
		JPanel buttonPanel = new CommonJPanel(TYPE_TRANSPARENT);
		buttonPanel.setLayout(new GridLayout(1, 2, 80, 0));
		add(buttonPanel);

		// 创建修改
		editBtn = new JButton("修改");
		buttonPanel.add(editBtn);

		// 创建重置按钮
		deleteBtn = new JButton("删除");
		buttonPanel.add(deleteBtn);

		// 添加按钮事件
		addListener();

	}

	/**
	 * 添加按钮事件
	 */
	public void addListener() {
		// 搜索按钮
		searchButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				//				User user = new User();
				//				user.setId(userIdField.getText());
				//				user.setUserName(userNameField.getText());
				//				UserTableModel.userList = (ArrayList<User>) userServiceImpl.getUserList(user);
				//				table.updateUI();
				//				resetInput();
				updateData();
				resetInput();
			}

		});
		/**
		 * @ 表格点击事件
		 */
		table.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				User user = getSelectUser();
				editUserIdField.setText(user.getId());
				editUserNameField.setText(user.getUserName());
				editUserPassField.setText(user.getPassword());
				roleComboBox.setSelectedItem(user.getAdmin());
				userTypeComboBoxEidt.setSelectedItem(user.getType());
				userTypeComboBoxEidt.updateUI();
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		//修改事件
		editBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (ComonUtil.isEmpty(editUserIdField.getText()) || ComonUtil.isEmpty(editUserNameField.getText())
						|| ComonUtil.isEmpty(editUserPassField.getText())
						|| null == roleComboBox.getSelectedItem()
						|| null == userTypeComboBoxEidt.getSelectedItem()) {
					JOptionPane.showMessageDialog(EditUserView.this, "选项不能为空！");
					return;
				}
				
				
				User user = new User();
				user.setId(editUserIdField.getText());
				user.setUserName(editUserNameField.getText());
				user.setPassword(editUserPassField.getText());
				user.setType(userTypeComboBoxEidt.getSelectedItem().toString());
				user.setAdmin( roleComboBox.getSelectedIndex());
				String flag ="更新失败！";
				if(userServiceImpl.upUser(user)) {
					flag = "更新成功！";
					updateData();
				}
				JOptionPane.showMessageDialog(EditUserView.this, flag);

			}
		});
		// 删除
		deleteBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (0 == editUserIdField.getText().length()) {
					JOptionPane.showMessageDialog(EditUserView.this, "请先选择一条记录！");
					return;
				}
				
				int choice = JOptionPane.showConfirmDialog(EditUserView.this, "是否删除？");
				if (choice != JOptionPane.OK_OPTION) {
					return;
				}
				
				String flag ="删除失败！";
				if(userServiceImpl.deleteUser(editUserIdField.getText())) {
					flag = "删除成功！";
					updateData();
					resetInput();
				}
				JOptionPane.showMessageDialog(EditUserView.this, flag);

			}
		});
	}

	/**
	 * 重置输入框内容
	 */
	public void resetInput() {
		editUserNameField.setText("");
		editUserIdField.setText("");
		editUserPassField.setText("");
		roleComboBox.setSelectedIndex(0);
		userTypeComboBoxEidt.setSelectedIndex(0);
		userTypeComboBoxEidt.updateUI();
	}
	
	/**
	 * @ 获取到当前选着行的表格值
	 */
	public User getSelectUser() {
		int row = table.getSelectedRow();
		return UserTableModel.userList.get(row);
	}

	/**
	 * 更新数据
	 */
	public void updateData() {
		User user = new User();
		user.setId(userIdField.getText());
		user.setUserName(userNameField.getText());
		UserTableModel.userList = (ArrayList<User>) userServiceImpl.getUserList(user);
		table.updateUI();
		//		resetInput();	
	}

}
